package essvc

import (
	"errors"
	"github.com/spf13/viper"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver"
	v1 "gluenet/pkg/log/v1"
)

func init() {
	depend.Provide(NewEsConfig)
	depend.Provide(NewService)
}

type Service struct {
	logger v1.Logger
	esCfg  *EsConfig
}

func NewService(esCfg *EsConfig) *Service {
	return &Service{
		logger: v1.NewLogger(""),
		esCfg:  esCfg,
	}
}

const (
	keyEsAddr = driver.KeyDriverPerfix + `es.addr`
)

type EsConfig struct {
	Addr string
}

func NewEsConfig(vp *viper.Viper) (*EsConfig, error) {
	addr := vp.GetString(keyEsAddr)
	if addr == "" {
		return nil, errors.New("empty elasticsearch addr")
	}

	return &EsConfig{
		Addr: addr,
	}, nil
}
